package com.tencent.wxcloudrun.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("yg")
public class YingGaoDapingController {

    @GetMapping("/daping")
    public Object daping() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
            // 获取 resources/doc 目录下的 JSON 文件路径
        File file = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("doc/data.json")).getFile());
        return objectMapper.readValue(file, Object.class);
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        // 获取 resources/doc 目录下的 JSON 文件路径

        File file = new File(Objects.requireNonNull(YingGaoDapingController.class.getClassLoader().getResource("doc/data.json")).getFile());
// 读取 JSON 文件
        JsonNode rootNode = objectMapper.readTree(file);

        // 获取 rows 数据
        JsonNode rowsNode = rootNode.path("rows");

        // 用于统计 TypeName 的 Map
        Map<String, Integer> typeNameCountMap = new HashMap<>();

        // 遍历 rows 数组，统计每个 TypeName 的个数
        for (JsonNode row : rowsNode) {
            String typeName = row.path("TypeName").asText();
            typeNameCountMap.put(typeName, typeNameCountMap.getOrDefault(typeName, 0) + 1);
        }


        // 打印统计结果
        for (Map.Entry<String, Integer> entry : typeNameCountMap.entrySet()) {
            System.out.println("TypeName: " + entry.getKey() + ", Count: " + entry.getValue());
        }
    }

}
